//
//  MessageCell.m
//  聊天布局
//
//  Created by J on 16/3/3.
//  Copyright © 2016年 J. All rights reserved.
//

#import "MessageCell.h"
#import "Message.h"

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"


@interface MessageCell()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *rightIconView;

@property (weak, nonatomic) IBOutlet UIImageView *leftIconView;
@property (weak, nonatomic) IBOutlet UIButton *textButton;
@property (weak, nonatomic) IBOutlet UIButton *leftTextButton;



@end
@implementation MessageCell


- (void)setMessage:(Message *)message{
    
    _message = message;
    self.timeLabel.text = message.time;
    
    //    self.timeLabel.hidden = message.hideTime;//是否隐藏时间
    if (message.hideTime) {//如果隐藏 设置约束为高度
        [self.timeLabel updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(0);
        }];
    }else{
        [self.timeLabel updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(21);
        }];
    }
    if (message.type == XMGMessageTypeMe) {//右边
        [self settingShowTextButton:self.textButton showIconView:self.rightIconView hiddenTextButton:self.leftTextButton hiddenIconView:self.leftIconView messate:_message];//传入需要显示和隐藏的按钮和模型

    }else{//左边
        
        [self settingShowTextButton:self.leftTextButton showIconView:self.leftIconView hiddenTextButton:self.textButton hiddenIconView:self.rightIconView messate:_message];
    }
    

    
}

- (void)settingShowTextButton:(UIButton*)showTextButton showIconView:(UIImageView*)showIconView hiddenTextButton:(UIButton*)hiddenTextButton hiddenIconView:(UIImageView*)hiddenIconView messate:(Message *)message{
    
    hiddenIconView.hidden = YES;
    hiddenTextButton.hidden = YES;
    showTextButton.hidden = NO;
    showIconView.hidden = NO;
    
    [showTextButton setTitle:message.text forState:UIControlStateNormal];//一旦设置了文字内容button.titleLabel就有文字了
    [showTextButton layoutIfNeeded];//先强制更新才能拿到设置文字后得正确高度
    //更新约束 因为按钮为循环利用的 如果约束存在覆盖 不存在添加
    [showTextButton mas_updateConstraints:^(MASConstraintMaker *make) {
        CGFloat buttonH = showTextButton.titleLabel.frame.size.height;
        make.height.equalTo(buttonH + 30) ;//让按钮的高度为内部label高度+30 label再内缩15
    }];
    [showTextButton layoutIfNeeded];//先强制更新才能拿到设置文字后得正确高度
    //计算当前cell的行高
    CGFloat buttonMaxY = CGRectGetMaxY(showTextButton.frame);
    CGFloat iconMaxY = CGRectGetMaxY(showIconView.frame);
    message.cellHeight = MAX(buttonMaxY, iconMaxY)+10;//系统自带获取两个最大值
}
                           
- (void)awakeFromNib {
    [super awakeFromNib];
//    self.textButton.contentEdgeInsets = UIEdgeInsetsMake(20, 10, 10, 10);//内容(imageView label)内缩 顶部 10 永远不显示内容除了背景图片
    self.textButton.titleLabel.numberOfLines = 0;
    self.leftTextButton.titleLabel.numberOfLines = 0;

    self.textButton.titleLabel.preferredMaxLayoutWidth = 100;
    self.leftTextButton.titleLabel.preferredMaxLayoutWidth = 100;

//    self.textButton.titleLabel.backgroundColor = [UIColor orangeColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
